package com.zed.api.common;

/**
 * 顺序号感知器
 * @author Zed
 *
 */
public interface SequenceNumberAware {

	/**
	 * 设置次序号生成器
	 * @param sequenceNumber {@link SequenceNumber}
	 */
	void setSerialNumber(SequenceNumber sequenceNumber);
	
	/**
	 * 尝试绑定次序号流水器
	 * @param <T>
	 * @param target {@link T},目标对象
	 * @param sequenceNumber {@link SequenceNumber}
	 * @return {@link boolean},返回{@code true}表示{@code target}可调用{@link #setSerialNumber(SequenceNumber)}方法。
	 */
	static <T> boolean maybeToBind(T target, SequenceNumber sequenceNumber) {
		if (target == null) return false;
		if (target instanceof SequenceNumberAware) {
			((SequenceNumberAware)target).setSerialNumber(sequenceNumber);
			return true;
		}
		
		return false;
	}
}
